package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.FactoryDao;
import cn.itcast.domain.cargo.Factory;
import cn.itcast.domain.cargo.FactoryExample;
import cn.itcast.service.cargo.FactoryService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class FactoryServiceImpl implements FactoryService {

    @Autowired
    private FactoryDao factoryDao;

    /*
        工厂分页查询
     */
    @Override
    public PageInfo<Factory> findByPage(FactoryExample factoryExample, int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);

        //查询数据
        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);

        //得到工厂页面
        PageInfo<Factory> pageInfo = new PageInfo<>(factoryList);


        return pageInfo;
    }

    /*
     工厂条件查询
  */
    @Override
    public List<Factory> findAll(FactoryExample factoryExample) {
        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);
        return factoryList;
    }

      /*
        根据id查询工厂
    */

    @Override
    public Factory findById(String id) {

        return factoryDao.selectByPrimaryKey(id);
    }

       /*
      添加工厂
  */

    @Override
    public void save(Factory factory) {

        factory.setId(UUID.randomUUID().toString());

        factory.setCreateTime(new Date());
        factory.setUpdateTime(new Date());

        factoryDao.insertSelective(factory);

    }

    /*
 更新工厂
  */
    @Override
    public void update(Factory factory) {

        factory.setUpdateTime(new Date());
        factoryDao.updateByPrimaryKeySelective(factory);
    }

     /*
    根据主键删除工厂
     */

    @Override
    public void delete(String id) {
        factoryDao.deleteByPrimaryKey(id);

    }


    /**
     * //根据厂家名字查找厂家
     */
    @Override
    public Factory findByFactoryName(String factoryName) {
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andFactoryNameEqualTo(factoryName);

        List<Factory> factoryList = factoryDao.selectByExample(factoryExample);

        //只有去到一个厂家
        Factory factory = factoryList.get(0);

        return factory;


    }
}



